#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_CommunicationControl_0x28_test.py
@Time    :   2022/08/29 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume
 
import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True


communicationControl_NRC_Phy = [
                                 ([0x28],[0x7F,0x28,0x13]),                    
                                 ([0x28,0x00],[0x7F,0x28,0x13]),
                                 ([0x28,0x01],[0x7F,0x28,0x13]),
                                 ([0x28,0x00,0x01,0x15],[0x7F,0x28,0x13]),
                                 ([0x28,0x00,0x03,0xFF],[0x7F,0x28,0x13]),  
                                 ([0x28,0x01,0x01,0x25],[0x7F,0x28,0x13]),
                                 ([0x28,0x01,0x03,0x06],[0x7F,0x28,0x13]),     
                                 ([0x28,0x02,0x01],[0x7F,0x28,0x12]),         
                                 ([0x28,0x02,0x03],[0x7F,0x28,0x12]),
                                 ([0x28,0x04,0x01],[0x7F,0x28,0x12]),         
                                 ([0x28,0x04,0x03],[0x7F,0x28,0x12]),
                                 ([0x28,0x05,0x01],[0x7F,0x28,0x12]),         
                                 ([0x28,0x05,0x03],[0x7F,0x28,0x12]),
                                 ([0x28,0x06,0x01],[0x7F,0x28,0x12]),        
                                 ([0x28,0x06,0x03],[0x7F,0x28,0x12]),
                                 ([0x28,0x3F,0x01],[0x7F,0x28,0x12]),         
                                 ([0x28,0x3F,0x03],[0x7F,0x28,0x12]),
                                 ([0x28,0x40,0x01],[0x7F,0x28,0x12]),         
                                 ([0x28,0x40,0x03],[0x7F,0x28,0x12]),
                                 ([0x28,0x5F,0x01],[0x7F,0x28,0x12]),         
                                 ([0x28,0x5F,0x03],[0x7F,0x28,0x12]),
                                 ([0x28,0x60,0x01],[0x7F,0x28,0x12]),         
                                 ([0x28,0x60,0x03],[0x7F,0x28,0x12]),
                                 ([0x28,0x7E,0x01],[0x7F,0x28,0x12]),         
                                 ([0x28,0x7E,0x03],[0x7F,0x28,0x12]),
                                 ([0x28,0x7F,0x01],[0x7F,0x28,0x12]),         
                                 ([0x28,0x7F,0x03],[0x7F,0x28,0x12]),
                                 ([0x28,0x02,0x02],[0x7F,0x28,0x12]),
                                 ([0x28,0x00,0x02],[0x7F,0x28,0x31]),     
                                 ([0x28,0x01,0x02],[0x7F,0x28,0x31]),
                                 ([0x28,0x00,0x04],[0x7F,0x28,0x31]),             
                                 ([0x28,0x01,0x04],[0x7F,0x28,0x31]),
                                 ([0x28,0x00,0x7F],[0x7F,0x28,0x31]),           
                                 ([0x28,0x01,0x7F],[0x7F,0x28,0x31])]

# 20220913 Revise:BYD 企标规定 功能寻址 NRC=11 12 13 7E 7F 31都不响应
communicationControl_NRC13_Fun = [([0x28],[]),                #[0x7F,0x28,0x13]),                    
                                  ([0x28,0x00],[]),           #[0x7F,0x28,0x13]),
                                  ([0x28,0x01],[]),           #[0x7F,0x28,0x13]),
                                  ([0x28,0x03],[]),           #[0x7F,0x28,0x13]),
                                  ([0x28,0x00,0x01,0x15],[]), #[0x7F,0x28,0x13]),
                                  ([0x28,0x00,0x03,0xFF],[]), #[0x7F,0x28,0x13]),  
                                  ([0x28,0x01,0x01,0x25],[]), #[0x7F,0x28,0x13]),
                                  ([0x28,0x01,0x03,0x06],[]), #[0x7F,0x28,0x13]),
                                  ([0x28,0x03,0x03,0x06],[]), #[0x7F,0x28,0x13]),
                                  ([0x28,0x03,0x01,0x25],[])] #[0x7F,0x28,0x13])]

communicationControl = [    ([0x28,0x00,0x01],[0x68,0x00]),         
                            ([0x28,0x01,0x01],[0x68,0x01]),
                            ([0x28,0x03,0x01],[0x68,0x03])]

communicationControl_NRC7F = [([0x28,0x00,0x01],[0x7F,0x28,0x7F]),         
                              ([0x28,0x01,0x01],[0x7F,0x28,0x7F]),
                              ([0x28,0x03,0x01],[0x7F,0x28,0x7F]),
                              ([0x28,0x80,0x01],[0x7F,0x28,0x7F]),         
                              ([0x28,0x81,0x01],[0x7F,0x28,0x7F]),
                              ([0x28,0x83,0x01],[0x7F,0x28,0x7F])]

communicationControl_SuppressPosMsg_NRC_Phy = [([0x28,0x80],[0x7F,0x28,0x13]),
                                               ([0x28,0x81],[0x7F,0x28,0x13]),
                                               ([0x28,0x80,0x01,0x15],[0x7F,0x28,0x13]),
                                               ([0x28,0x80,0x03,0xFF],[0x7F,0x28,0x13]),  
                                               ([0x28,0x81,0x01,0x25],[0x7F,0x28,0x13]),
                                               ([0x28,0x81,0x03,0x06],[0x7F,0x28,0x13]),     
                                               ([0x28,0x82,0x01],[0x7F,0x28,0x12]),         
                                               ([0x28,0x84,0x01],[0x7F,0x28,0x12]),         
                                               ([0x28,0x84,0x03],[0x7F,0x28,0x12]),
                                               ([0x28,0x85,0x01],[0x7F,0x28,0x12]),         
                                               ([0x28,0x85,0x03],[0x7F,0x28,0x12]),
                                               ([0x28,0x86,0x01],[0x7F,0x28,0x12]),        
                                               ([0x28,0x86,0x03],[0x7F,0x28,0x12]),
                                               ([0x28,0xBF,0x01],[0x7F,0x28,0x12]),         
                                               ([0x28,0xBF,0x03],[0x7F,0x28,0x12]),
                                               ([0x28,0xC0,0x01],[0x7F,0x28,0x12]),         
                                               ([0x28,0xC0,0x03],[0x7F,0x28,0x12]),
                                               ([0x28,0xDF,0x01],[0x7F,0x28,0x12]),         
                                               ([0x28,0xDF,0x03],[0x7F,0x28,0x12]),
                                               ([0x28,0xE0,0x01],[0x7F,0x28,0x12]),         
                                               ([0x28,0xE0,0x03],[0x7F,0x28,0x12]),
                                               ([0x28,0xFE,0x01],[0x7F,0x28,0x12]),         
                                               ([0x28,0xFE,0x03],[0x7F,0x28,0x12]),
                                               ([0x28,0xFF,0x01],[0x7F,0x28,0x12]),         
                                               ([0x28,0xFF,0x03],[0x7F,0x28,0x12]),
                                               ([0x28,0x82,0x02],[0x7F,0x28,0x12]),
                                               ([0x28,0x80,0x02],[0x7F,0x28,0x31]),     
                                               ([0x28,0x81,0x02],[0x7F,0x28,0x31]),
                                               ([0x28,0x83,0x02],[0x7F,0x28,0x31]),
                                               ([0x28,0x80,0x04],[0x7F,0x28,0x31]),             
                                               ([0x28,0x81,0x04],[0x7F,0x28,0x31]),
                                               ([0x28,0x83,0x04],[0x7F,0x28,0x31]),
                                               ([0x28,0x80,0x7F],[0x7F,0x28,0x31]),           
                                               ([0x28,0x81,0x7F],[0x7F,0x28,0x31]),
                                               ([0x28,0x83,0x7F],[0x7F,0x28,0x31])]

communicationControl_SuppressPosMsg_NRC13_Fun = [                   
                                                  ([0x28,0x80],[]),             #[0x7F,0x28,0x13]),
                                                  ([0x28,0x81],[]),             #[0x7F,0x28,0x13]),
                                                  ([0x28,0x80,0x01,0x15],[]),   #[0x7F,0x28,0x13]),
                                                  ([0x28,0x80,0x03,0xFF],[]),   #[0x7F,0x28,0x13]),  
                                                  ([0x28,0x81,0x01,0x25],[]),   #[0x7F,0x28,0x13]),
                                                  ([0x28,0x81,0x03,0x06],[])]   #[0x7F,0x28,0x13])]

communicationControl_SuppressPosMsg_No_Resp_Fun = [([0x28,0x80,0x01],[]),         
                                                    ([0x28,0x81,0x01],[]),
                                                    ([0x28,0x83,0x01],[])]



class Test_CommunicationControl_Phy(object):
    '''雷达没有发送任何应用报文'''

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in communicationControl] 
    @allure.severity('normal')
    @allure.story('Communication Control Test')
    @allure.description('Test positive response of 0x28')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp',communicationControl,ids=ids)
    def test_CommunicationControl_programming_Phy(self,action,Driver, req,resp):
        iStep = 1
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 2, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)

        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
             

    @allure.severity('normal')
    @allure.description('Test Function failure of 0x28')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_CommunicationControl_Function_failure_programming_Phy(self,action,Driver):
        iStep = 1
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 2, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x28,0x01,0x01], [0x68,0x01], 0, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 3, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 3, iStep, False)

        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10,0x01], [0x50,0x01], 0, iStep, False)

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in communicationControl_NRC_Phy]   
    @allure.severity('minor')         
    @allure.description('Test negative response code of 0x28 when suppressPosMsgBit is enabled with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp',communicationControl_NRC_Phy,ids=ids)
    def test_CommunicationControl_NRC_programming_Phy(self,action,Driver,req,resp):
        iStep = 1
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)


    @allure.severity('normal')
    @allure.description('Test 0x28 when suppressPosMsgBit is enabled')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_CommunicationControl_SuppressPosMsgBit_programming_Phy(self,action,Driver):
        iStep = 1
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 2, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x28,0x81,0x01], [], 0, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)


        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x28,0x80,0x01], [], 0, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)
             



    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in communicationControl_SuppressPosMsg_NRC_Phy] 
    @allure.severity('normal')         
    @allure.description('Test negative response code of 0x28 when suppressPosMsgBit is enabled with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp',communicationControl_SuppressPosMsg_NRC_Phy,ids=ids)
    def test_CommunicationControl_SuppressPosMsgBit_NRC_programmming_Phy(self,action,Driver,req,resp):
        iStep = 1
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)